﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApplication1.RssFeedService;

namespace WPFClient
{
    /// <summary>
    /// Interaction logic for rssFeed.xaml
    /// </summary>
    public partial class rssFeed : Window
    {
        public const String ERROR_EMPTY_FEED = "La liste de flux RSS est vide";
        public utils u;

        /// ///////////////////////////////////////////////////////
        /// 
        ///                   Constructeur
        /// 
        /// //////////////////////////////////////////////////////
        public rssFeed(utils _u)
        {
            InitializeComponent();
            this.Visibility = System.Windows.Visibility.Hidden;
            u = _u;
            u.RssDico = new Dictionary<int, RssList>();
            u.RssPublicDico = new Dictionary<int, RssList>();
            u.rssWin = this;
        }


        /// ///////////////////////////////////////////////////////
        /// 
        ///              Methodes appels au serveur
        /// 
        /// //////////////////////////////////////////////////////
        /// 
        /// <summary>
        /// Effectue les actions en retour de l'appel ListRssFeed
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event avec les parametres de retour</param>
        public void rssFeed_ListRssFeedCompleted(Object sender, ListRssFeedCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                if (this.u.RssDico.Count != 0)
                {
                    this.u.RssDico.Clear();
                    listBox1.Items.Clear();
                } 
                List<RssFeed> l = e.Result.ToList() as List<RssFeed>;
                int i = 0;
                foreach (RssFeed r in l)
                {
                    RssList rss = new RssList();
                    rss.id = r.id;
                    rss.feed = r;
                    rss.name = r.title;
                    this.u.RssDico.Add(i, rss);
                    i++;
                }
                i = 0;
                if (listBox1.Items.Count != u.RssDico.Count)
                {
                    listBox1.Items.Clear();
                    while (i != u.RssDico.Count)
                    {
                        listBox1.Items.Add(u.RssDico[i].name);
                        i++;
                    }
                }
            }
            else
            {
                lError.Content = ERROR_EMPTY_FEED;
                lError.Visibility = System.Windows.Visibility.Visible;
            }
        }

        public void listPublicFeed()
        {
            u.getRfs().ListPublicRssFeedCompleted += new EventHandler<ListPublicRssFeedCompletedEventArgs>(rssFeed_ListPublicRssFeedCompleted);
            u.getRfs().ListPublicRssFeedAsync(u.getSession());
        }

        /// <summary>
        /// Effectue les actions en retour de l'appel ListPublicRssFeed
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event avec le resultat de la requete</param>
        public void rssFeed_ListPublicRssFeedCompleted(Object sender, ListPublicRssFeedCompletedEventArgs e)
        {
            listBox1.Items.Clear();
            if (e.Result != null)
            {
                if (this.u.RssPublicDico.Count != 0)
                {
                    this.u.RssPublicDico.Clear();
                    listBox1.Items.Clear();
                } 
                List<RssFeed> l = e.Result.ToList() as List<RssFeed>;
                int i = 0;
                foreach (RssFeed r in l)
                {
                    if (r.@public == true)
                    {
                        RssList rss = new RssList();
                        rss.id = r.id;
                        rss.feed = r;
                        rss.name = r.title;
                        this.u.RssPublicDico.Add(i, rss);
                    }
                    i++;
                }
                i = 0;
                if (listBox1.Items.Count != u.RssPublicDico.Count)
                {
                    listBox1.Items.Clear();
                    while (i != u.RssPublicDico.Count)
                    {
                        listBox1.Items.Add(u.RssPublicDico[i].name);
                        i++;
                    }
                }
            }
            else
            {
                lError.Content = ERROR_EMPTY_FEED;
                lError.Visibility = System.Windows.Visibility.Visible;
            }
        }

        public void listFeed()
        {
            u.getRfs().ListRssFeedCompleted += new EventHandler<ListRssFeedCompletedEventArgs>(rssFeed_ListRssFeedCompleted);
            u.getRfs().ListRssFeedAsync(u.getSession());
        }

        

        /// ///////////////////////////////////////////////////////
        /// 
        ///                     Subscribe
        /// 
        /// //////////////////////////////////////////////////////
        public void Subscribe()
        {
            subscribeWin s = new subscribeWin(u);
        }


        
        /// <summary>
        /// Ferme toutes les fenetres et clos la connexion avec le server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rfs_endSessionCompleted(Object sender, endSessionCompletedEventArgs e)
        {
            this.Close();
            u._win.Close();
        }

        /// ///////////////////////////////////////////////////////
        /// 
        ///                     ListRssFeedItem
        /// 
        /// //////////////////////////////////////////////////////

        public void rssFeed_ListRssFeedItemCompleted(Object sender, ListRssFeedItemCompletedEventArgs e)
        {
            List<RssFeedItem> l = e.Result.ToList() as List<RssFeedItem>;
            u.RssDico[u.current].items = l;
        }

        public void ListRssFeedItem(RssFeed feed)
        {
            u.getRfs().ListRssFeedItemCompleted += new EventHandler<ListRssFeedItemCompletedEventArgs>(rssFeed_ListRssFeedItemCompleted);
            u.getRfs().ListRssFeedItemAsync(u.getSession(), feed);
        }


        /// ///////////////////////////////////////////////////////
        /// 
        ///                     Boutons
        /// 
        /// //////////////////////////////////////////////////////

        private void disconnect_Click(object sender, RoutedEventArgs e)
        {
            u.getRfs().endSessionCompleted += new EventHandler<endSessionCompletedEventArgs>(rfs_endSessionCompleted);
            u.getRfs().endSessionAsync(u.getSession());
        }

        private void bflux_Click(object sender, RoutedEventArgs e)
        {
            if (lError.Visibility != System.Windows.Visibility.Collapsed)
                lError.Visibility = System.Windows.Visibility.Collapsed;
            this.listFeed();
        }

        private void bfluxpublics_Click(object sender, RoutedEventArgs e)
        {
            if (lError.Visibility != System.Windows.Visibility.Collapsed)
                lError.Visibility = System.Windows.Visibility.Collapsed;
            this.listPublicFeed();
        }

        private void bsubscribe_Click(object sender, RoutedEventArgs e)
        {
            Subscribe();
        }

        /// ///////////////////////////////////////////////////////
        /// 
        ///                     Listbox
        /// 
        /// //////////////////////////////////////////////////////

        private void listBox_selectionChanged(object sender, System.EventArgs e)
        {
            int j = 0;
            while (j != u.RssDico.Count)
            {
                u.current = j;
                ListRssFeedItem(u.RssDico[j].feed);
                j++;
            }
            RssList l = u.getRssListByRssFeedName(listBox1.SelectedItem.ToString());
            listBox2.Items.Clear();
            foreach (RssFeedItem i in l.items)
                listBox2.Items.Add(i.title);
        }
    }
}
